package com.midea.base;

/**
 * @Author: Mr.Wang
 * @Description:计算一个数的二进制表示中有多少个1
 * https://blog.csdn.net/yilouwen7522/article/details/80775545
 * 每次计算一次n & (n-1) ，就代表二进制v中有几个1
 * @Date: Create in 16:55 2021/8/29
 */
public class Count1 {
    public static void main(String[] args) {
        int n = 127;
        System.out.println(Integer.toBinaryString(n));
        System.out.println(count((byte)n));
    }

    static int count(byte v) {
        int num = 0;
        while (v != 0) {
            v &= (v - 1);
            num++;
        }
        return num;
    }
}
